package com.supcon.exchangedata.service.sw;

import com.alibaba.fastjson.JSON;
import com.supcon.exchangedata.pojo.entity.mysql.operation.sw.SwStatistOtherParametersMonth;
import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoArea;
import com.supcon.exchangedata.pojo.entity.oracle.CoordinateReplaceParam;
import com.supcon.exchangedata.pojo.entity.oracle.GisPoint;
import com.supcon.exchangedata.pojo.entity.Response;
import com.supcon.exchangedata.pojo.entity.mysql.operation.sw.SwBasicInfo;
import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersMonth;
import com.supcon.exchangedata.repository.master.MasterBaseAreaRepository;
import com.supcon.exchangedata.repository.master.MasterGisPointRepository;
import com.supcon.exchangedata.repository.master.MasterStatistOtherParametersMonthRepository;
import com.supcon.exchangedata.repository.slave1.SlaveOneStatistOtherParametersDayRepository;
import com.supcon.exchangedata.repository.slave2.SlaveTwoGasDailyOperationInfo;
import com.supcon.exchangedata.repository.slave3.SlaveSwOperationInfo3;
import com.supcon.exchangedata.repository.slave3.SwStatistOtherParametersMonthInfo;
import com.supcon.exchangedata.utils.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/12/22/10:17
 * @Description:
 */
@Service
@Slf4j
public class SwBasicService {
    //坐标转换
    private final static String TRAN_URL = "http://10.110.112.226:8094/local/api/v1/coordinate/transReverseBatch";
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private MasterBaseAreaRepository masterBaseAreaRepository;
    @Resource
    private MasterGisPointRepository masterGisPointRepository;
    @Resource
    private SlaveSwOperationInfo3 slaveSwOperationInfo3;
    @Resource
    private MasterStatistOtherParametersMonthRepository monthRepository;
    @Resource
    private SwStatistOtherParametersMonthInfo swStatistOtherParametersMonthInfo;

    public void swBasicInfo() {
        //供水区域 102 2004    排水 202  2005   供水泵站 105  2006 污水泵站 203 2009
        List<BaseGeoArea> baseGeoAreasByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(2004L);
        List<SwBasicInfo> allByAreaType = slaveSwOperationInfo3.findAllByAreaType("102");
        handleData(baseGeoAreasByAreaType, allByAreaType, 2004L, 20941L);

        //
        List<BaseGeoArea> baseGeoAreasByAreaType1 = masterBaseAreaRepository.findBaseGeoAreaByAreaType(2005L);
        List<SwBasicInfo> allByAreaType1 = slaveSwOperationInfo3.findAllByAreaType("202");
        handleData(baseGeoAreasByAreaType1, allByAreaType1, 2005L, 20943L);

        //
        List<BaseGeoArea> baseGeoAreasByAreaType2 = masterBaseAreaRepository.findBaseGeoAreaByAreaType(2006L);
        List<SwBasicInfo> allByAreaType2 = slaveSwOperationInfo3.findAllByAreaType("105");
        handleData(baseGeoAreasByAreaType2, allByAreaType2, 2006L, 20944L);

        //
        List<BaseGeoArea> baseGeoAreasByAreaType3 = masterBaseAreaRepository.findBaseGeoAreaByAreaType(2009L);
        List<SwBasicInfo> allByAreaType3 = slaveSwOperationInfo3.findAllByAreaType("203");
        handleData(baseGeoAreasByAreaType3, allByAreaType3, 2009L, 20909L);

    }

    @Transactional(rollbackFor = Exception.class)
    public void handleData(List<BaseGeoArea> baseGeoAreasByAreaType, List<SwBasicInfo> allByAreaType, Long areaType, Long parentId) {
        if (!CollectionUtils.isEmpty(allByAreaType)) {
            CoordinateReplaceParam coordinateReplaceParam = new CoordinateReplaceParam();
            List<double[]> list = new ArrayList<>();
            List<BaseGeoArea> addList = new ArrayList<>();
            List<BaseGeoArea> edit = new ArrayList<>();
            List<BaseGeoArea> remove = new ArrayList<>();
            if (CollectionUtils.isEmpty(baseGeoAreasByAreaType)) {

                for (SwBasicInfo swBasicInfo : allByAreaType) {
                    BaseGeoArea baseGeoArea = new BaseGeoArea();
                    baseGeoArea.setAreaCode(swBasicInfo.getAreaCode());
                    baseGeoArea.setAreaName(swBasicInfo.getAreaName());
                    baseGeoArea.setParentId(parentId);
                    baseGeoArea.setAreaType(areaType);
                    baseGeoArea.setRemarks(swBasicInfo.getRemarks());
                    baseGeoArea.setExtension2(swBasicInfo.getLatitude());
                    baseGeoArea.setExtension3(swBasicInfo.getLongitude());
                    baseGeoArea.setTrueName(swBasicInfo.getAreaName());
                    if (!StringUtils.isEmpty(swBasicInfo.getLatitude()) && !StringUtils.isEmpty(swBasicInfo.getLongitude())) {
                        double[] a = new double[]{Double.parseDouble(swBasicInfo.getLongitude()), Double.parseDouble(swBasicInfo.getLatitude())};
                        list.add(a);
                        coordinateReplaceParam.setData(list);
                        String s = JSON.toJSONString(coordinateReplaceParam);
                        List<double[]> doubles = transReverseBatch(s);
                        double[] doubles1 = doubles.get(0);
                        baseGeoArea.setLatitude(doubles1[1]);
                        baseGeoArea.setLongitude(doubles1[0]);
                        list.clear();
                    }
                    String planData = swBasicInfo.getPlanData();
                    switch (swBasicInfo.getAreaName()) {
                        case "主城区供水":
                            baseGeoArea.setAreaCode("sczcq001");
                            baseGeoArea.setCompanyNature("供水区域");
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                baseGeoArea.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                String remarks = swBasicInfo.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    baseGeoArea.setExtension5(substring);
                                }
                            }
                        case "主城区排水":
                            baseGeoArea.setAreaCode("zcqps000001");
                            baseGeoArea.setCompanyNature("排水区域");
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                baseGeoArea.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                String remarks = swBasicInfo.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    baseGeoArea.setExtension5(substring);
                                }
                            }
                        case "滨江区供水":
                            baseGeoArea.setAreaCode("scjycs27");
                            baseGeoArea.setCompanyNature("供水区域");
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                baseGeoArea.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                String remarks = swBasicInfo.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    baseGeoArea.setExtension5(substring);
                                }
                            }
                        case "富阳区供水":
                            baseGeoArea.setAreaCode("scjycs282");
                            baseGeoArea.setCompanyNature("供水区域");
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                baseGeoArea.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                String remarks = swBasicInfo.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    baseGeoArea.setExtension5(substring);
                                }
                            }
                        case "富阳区排水":
                            baseGeoArea.setAreaCode("wsc000421");
                            baseGeoArea.setCompanyNature("排水区域");
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                baseGeoArea.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                String remarks = swBasicInfo.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    baseGeoArea.setExtension5(substring);
                                }
                            }
                        case "临安区供水":
                            baseGeoArea.setAreaCode("sc0001");
                            baseGeoArea.setCompanyNature("供水区域");
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                baseGeoArea.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                String remarks = swBasicInfo.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    baseGeoArea.setExtension5(substring);
                                }
                            }
                        case "临安区排水":
                            baseGeoArea.setAreaCode("wsc0001");
                            baseGeoArea.setCompanyNature("排水区域");
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                baseGeoArea.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                String remarks = swBasicInfo.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    baseGeoArea.setExtension5(substring);
                                }
                            }
                        case "建德市供水":
                            baseGeoArea.setAreaCode("sc0006");
                            baseGeoArea.setCompanyNature("供水区域");
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                baseGeoArea.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                String remarks = swBasicInfo.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    baseGeoArea.setExtension5(substring);
                                }
                            }
                        case "建德市排水":
                            baseGeoArea.setAreaCode("wsc0003");
                            baseGeoArea.setCompanyNature("排水区域");
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                baseGeoArea.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                String remarks = swBasicInfo.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    baseGeoArea.setExtension5(substring);
                                }
                            }
                    }


                    addList.add(baseGeoArea);
                }

            } else {
                Iterator<BaseGeoArea> baseGeoAreaIterator = baseGeoAreasByAreaType.iterator();

                while (baseGeoAreaIterator.hasNext()) {
                    //code 或者trueName一致
                    BaseGeoArea next = baseGeoAreaIterator.next();
                    String areaCode = next.getAreaCode();
                    String trueName = next.getTrueName();
                    boolean removeFlag = true;
                    Iterator<SwBasicInfo> swBasicInfoIterator = allByAreaType.iterator();
                    while (swBasicInfoIterator.hasNext()) {
                        SwBasicInfo next1 = swBasicInfoIterator.next();
                        String areaCode1 = next1.getAreaCode();
                        String name = next1.getAreaName();
                        if (areaCode.equals(areaCode1) || (!StringUtils.isEmpty(trueName) && trueName.equals(name))) {

                            next.setRemarks(next1.getRemarks());
                            next.setExtension2(next1.getLatitude());
                            next.setExtension3(next1.getLongitude());
                            next.setTrueName(next1.getAreaName());
                            if (!StringUtils.isEmpty(next1.getLatitude()) && !StringUtils.isEmpty(next1.getLongitude())) {
                                double[] a = new double[]{Double.parseDouble(next1.getLongitude()), Double.parseDouble(next1.getLatitude())};
                                list.add(a);
                                coordinateReplaceParam.setData(list);
                                String s = JSON.toJSONString(coordinateReplaceParam);
                                List<double[]> doubles = transReverseBatch(s);
                                double[] doubles1 = doubles.get(0);
                                next.setLatitude(doubles1[1]);
                                next.setLongitude(doubles1[0]);
                                list.clear();
                            }
                            String planData = next1.getPlanData();
                            if (!StringUtils.isEmpty(planData)) {
                                BigDecimal bigDecimal = new BigDecimal(planData);
                                BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                next.setExtension1(divide.toPlainString());
                            }
                            if (!StringUtils.isEmpty(next1.getRemarks())) {
                                String remarks = next.getRemarks();
                                int startIndex = remarks.indexOf("共有");
                                int endIndex = remarks.indexOf("个");
                                if ((endIndex > startIndex) && startIndex >= 0) {
                                    String substring = remarks.substring(startIndex + 2, endIndex);
                                    next.setExtension5(substring);
                                }else {
                                    next.setExtension5("");
                                }
                            }
                            edit.add(next);
                            removeFlag = false;
                            //匹配成功,删除该项
                            swBasicInfoIterator.remove();
                            break;
                        }

                    }
                    //没有检测到更新中有本条数据，进入删除队列
                    if (removeFlag) {
                        remove.add(next);
                        baseGeoAreaIterator.remove();
                    }
                }

                //移除数据库中需要更新到本地库的数据后，剩下的处理成添加队列
                if (!CollectionUtils.isEmpty(allByAreaType)) {
                    for (SwBasicInfo swBasicInfo : allByAreaType) {
                        BaseGeoArea baseGeoArea = new BaseGeoArea();
                        baseGeoArea.setAreaCode(swBasicInfo.getAreaCode());
                        baseGeoArea.setAreaName(swBasicInfo.getAreaName());
                        baseGeoArea.setParentId(parentId);
                        baseGeoArea.setAreaType(areaType);
                        baseGeoArea.setRemarks(swBasicInfo.getRemarks());
                        baseGeoArea.setExtension2(swBasicInfo.getLatitude());
                        baseGeoArea.setExtension3(swBasicInfo.getLongitude());
                        baseGeoArea.setTrueName(swBasicInfo.getAreaName());
                        if (!StringUtils.isEmpty(swBasicInfo.getLatitude()) && !StringUtils.isEmpty(swBasicInfo.getLongitude())) {
                            double[] a = new double[]{Double.parseDouble(swBasicInfo.getLongitude()), Double.parseDouble(swBasicInfo.getLatitude())};
                            list.add(a);
                            coordinateReplaceParam.setData(list);
                            String s = JSON.toJSONString(coordinateReplaceParam);
                            List<double[]> doubles = transReverseBatch(s);
                            double[] doubles1 = doubles.get(0);
                            baseGeoArea.setLatitude(doubles1[0]);
                            baseGeoArea.setLongitude(doubles1[1]);
                            list.clear();
                        }
                        String planData = swBasicInfo.getPlanData();
                        switch (swBasicInfo.getAreaName()) {
                            case "主城区供水":
                                baseGeoArea.setAreaCode("sczcq001");
                                baseGeoArea.setCompanyNature("供水区域");
                                if (!StringUtils.isEmpty(planData)) {
                                    BigDecimal bigDecimal = new BigDecimal(planData);
                                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                    baseGeoArea.setExtension1(divide.toPlainString());
                                }
                                if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                    String remarks = swBasicInfo.getRemarks();
                                    int startIndex = remarks.indexOf("共有");
                                    int endIndex = remarks.indexOf("个");
                                    if ((endIndex > startIndex) && startIndex >= 0) {
                                        String substring = remarks.substring(startIndex + 2, endIndex);
                                        baseGeoArea.setExtension5(substring);
                                    }
                                }
                            case "主城区排水":
                                baseGeoArea.setAreaCode("zcqps000001");
                                baseGeoArea.setCompanyNature("排水区域");
                                if (!StringUtils.isEmpty(planData)) {
                                    BigDecimal bigDecimal = new BigDecimal(planData);
                                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                    baseGeoArea.setExtension1(divide.toPlainString());
                                }
                                if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                    String remarks = swBasicInfo.getRemarks();
                                    int startIndex = remarks.indexOf("共有");
                                    int endIndex = remarks.indexOf("个");
                                    if ((endIndex > startIndex) && startIndex >= 0) {
                                        String substring = remarks.substring(startIndex + 2, endIndex);
                                        baseGeoArea.setExtension5(substring);
                                    }
                                }
                            case "滨江区供水":
                                baseGeoArea.setAreaCode("scjycs27");
                                baseGeoArea.setCompanyNature("供水区域");
                                if (!StringUtils.isEmpty(planData)) {
                                    BigDecimal bigDecimal = new BigDecimal(planData);
                                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                    baseGeoArea.setExtension1(divide.toPlainString());
                                }
                                if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                    String remarks = swBasicInfo.getRemarks();
                                    int startIndex = remarks.indexOf("共有");
                                    int endIndex = remarks.indexOf("个");
                                    if ((endIndex > startIndex) && startIndex >= 0) {
                                        String substring = remarks.substring(startIndex + 2, endIndex);
                                        baseGeoArea.setExtension5(substring);
                                    }
                                }
                            case "富阳区供水":
                                baseGeoArea.setAreaCode("scjycs282");
                                baseGeoArea.setCompanyNature("供水区域");
                                if (!StringUtils.isEmpty(planData)) {
                                    BigDecimal bigDecimal = new BigDecimal(planData);
                                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                    baseGeoArea.setExtension1(divide.toPlainString());
                                }
                                if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                    String remarks = swBasicInfo.getRemarks();
                                    int startIndex = remarks.indexOf("共有");
                                    int endIndex = remarks.indexOf("个");
                                    if ((endIndex > startIndex) && startIndex >= 0) {
                                        String substring = remarks.substring(startIndex + 2, endIndex);
                                        baseGeoArea.setExtension5(substring);
                                    }
                                }
                            case "富阳区排水":
                                baseGeoArea.setAreaCode("wsc000421");
                                baseGeoArea.setCompanyNature("排水区域");
                                if (!StringUtils.isEmpty(planData)) {
                                    BigDecimal bigDecimal = new BigDecimal(planData);
                                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                    baseGeoArea.setExtension1(divide.toPlainString());
                                }
                                if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                    String remarks = swBasicInfo.getRemarks();
                                    int startIndex = remarks.indexOf("共有");
                                    int endIndex = remarks.indexOf("个");
                                    if ((endIndex > startIndex) && startIndex >= 0) {
                                        String substring = remarks.substring(startIndex + 2, endIndex);
                                        baseGeoArea.setExtension5(substring);
                                    }
                                }
                            case "临安区供水":
                                baseGeoArea.setAreaCode("sc0001");
                                baseGeoArea.setCompanyNature("供水区域");
                                if (!StringUtils.isEmpty(planData)) {
                                    BigDecimal bigDecimal = new BigDecimal(planData);
                                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                    baseGeoArea.setExtension1(divide.toPlainString());
                                }
                                if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                    String remarks = swBasicInfo.getRemarks();
                                    int startIndex = remarks.indexOf("共有");
                                    int endIndex = remarks.indexOf("个");
                                    if ((endIndex > startIndex) && startIndex >= 0) {
                                        String substring = remarks.substring(startIndex + 2, endIndex);
                                        baseGeoArea.setExtension5(substring);
                                    }
                                }
                            case "临安区排水":
                                baseGeoArea.setAreaCode("wsc0001");
                                baseGeoArea.setCompanyNature("排水区域");
                                if (!StringUtils.isEmpty(planData)) {
                                    BigDecimal bigDecimal = new BigDecimal(planData);
                                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                    baseGeoArea.setExtension1(divide.toPlainString());
                                }
                                if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                    String remarks = swBasicInfo.getRemarks();
                                    int startIndex = remarks.indexOf("共有");
                                    int endIndex = remarks.indexOf("个");
                                    if ((endIndex > startIndex) && startIndex >= 0) {
                                        String substring = remarks.substring(startIndex + 2, endIndex);
                                        baseGeoArea.setExtension5(substring);
                                    }
                                }
                            case "建德市供水":
                                baseGeoArea.setAreaCode("sc0006");
                                baseGeoArea.setCompanyNature("供水区域");
                                if (!StringUtils.isEmpty(planData)) {
                                    BigDecimal bigDecimal = new BigDecimal(planData);
                                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                    baseGeoArea.setExtension1(divide.toPlainString());
                                }
                                if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                    String remarks = swBasicInfo.getRemarks();
                                    int startIndex = remarks.indexOf("共有");
                                    int endIndex = remarks.indexOf("个");
                                    if ((endIndex > startIndex) && startIndex >= 0) {
                                        String substring = remarks.substring(startIndex + 2, endIndex);
                                        baseGeoArea.setExtension5(substring);
                                    }
                                }
                            case "建德市排水":
                                baseGeoArea.setAreaCode("wsc0003");
                                baseGeoArea.setCompanyNature("排水区域");
                                if (!StringUtils.isEmpty(planData)) {
                                    BigDecimal bigDecimal = new BigDecimal(planData);
                                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                                    baseGeoArea.setExtension1(divide.toPlainString());
                                }
                                if (!StringUtils.isEmpty(swBasicInfo.getRemarks())) {
                                    String remarks = swBasicInfo.getRemarks();
                                    int startIndex = remarks.indexOf("共有");
                                    int endIndex = remarks.indexOf("个");
                                    if ((endIndex > startIndex) && startIndex >= 0) {
                                        String substring = remarks.substring(startIndex + 2, endIndex);
                                        baseGeoArea.setExtension5(substring);
                                    }
                                }
                        }


                        addList.add(baseGeoArea);
                    }
                }


            }
            LocalDateTime now = LocalDateTime.now();

            //处理新增队列
            if (!CollectionUtils.isEmpty(addList)) {
                for (BaseGeoArea baseGeoArea : addList) {
                    baseGeoArea.setUpdateDate(now);
                    baseGeoArea.setUpdator("jpa-program");
                    BaseGeoArea save = masterBaseAreaRepository.save(baseGeoArea);
                    if (save.getLongitude() != null && save.getLatitude() != null) {
                        GisPoint gisPoint = new GisPoint();
                        gisPoint.setCode(save.getAreaCode());
                        gisPoint.setName(save.getAreaName());
                        gisPoint.setYwCategory(String.valueOf(save.getAreaType()));
                        gisPoint.setLongitude(save.getLongitude());
                        gisPoint.setLatitude(save.getLatitude());
                        gisPoint.setObjType(1L);
                        gisPoint.setObjCode(save.getAreaCode());
                        gisPoint.setBusinessCategory(String.valueOf(save.getAreaType()));
                        gisPoint.setObjId(save.getId());
                        masterGisPointRepository.save(gisPoint);
                    }
                }
            }

            //处理删除队列
            if (!CollectionUtils.isEmpty(remove)) {
                for (BaseGeoArea baseGeoArea : remove) {
                    masterBaseAreaRepository.deleteById(baseGeoArea.getId());
                    masterGisPointRepository.deleteByObjIdAndYwCategory(baseGeoArea.getId(), String.valueOf(baseGeoArea.getAreaType()));
                }
            }


            //处理编辑队列
            if (!CollectionUtils.isEmpty(edit)) {
                for (BaseGeoArea baseGeoArea : edit) {
                    baseGeoArea.setUpdateDate(now);
                    baseGeoArea.setUpdator("jpa-program");
                    BaseGeoArea save = masterBaseAreaRepository.save(baseGeoArea);
                    if (save.getLongitude() != null && save.getLatitude() != null) {
                        GisPoint gisPoint = new GisPoint();
                        gisPoint.setCode(save.getAreaCode());
                        gisPoint.setName(save.getAreaName());
                        gisPoint.setYwCategory(String.valueOf(save.getAreaType()));
                        gisPoint.setLongitude(save.getLongitude());
                        gisPoint.setLatitude(save.getLatitude());
                        gisPoint.setObjType(1L);
                        gisPoint.setObjCode(save.getAreaCode());
                        gisPoint.setBusinessCategory(String.valueOf(save.getAreaType()));
                        gisPoint.setObjId(save.getId());
                        try {
                            masterGisPointRepository.updateGisPointNew(save.getAreaCode(), save.getAreaName(), String.valueOf(save.getAreaType()),
                                    save.getLongitude(), save.getLatitude(), 1L, save.getAreaCode(), String.valueOf(save.getAreaType()), save.getId());
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                    }
                }
            }


        }


        //先把两者转为map


    }

    public void gsUser(){
        Calendar instance = Calendar.getInstance();
        Date time = instance.getTime();
        SimpleDateFormat simpleDateFormats = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormats.format(time);
        String years = Years(format);
        Timestamp timestamp = DateTimeUtils.localDateTimeToTimeSTamp(LocalDateTime.now());
        //插入前先查询前置库是否存在当月数据
        List<SwStatistOtherParametersMonth> list = swStatistOtherParametersMonthInfo.findAll();
        if (CollectionUtils.isEmpty(list)){
            List<StatistOtherParametersMonth> allByType = monthRepository.findAllByType("20031");
            log.info("查询原始数据集合: ",allByType);
            allByType.stream().forEach(v->{
                SwStatistOtherParametersMonth sw = new SwStatistOtherParametersMonth();
                sw.setTongTime(timestamp);
                sw.setBizTime(new Date());
                sw.setOp("insert");
                sw.setAvgValue(v.getInAvgValue().toString());
                sw.setMonthKey(years);
                list.add(sw);
            });
            swStatistOtherParametersMonthInfo.saveAll(list);
        }else {
            SwStatistOtherParametersMonth byTime = swStatistOtherParametersMonthInfo.findByTime(years);
            if (null == byTime) {
                //已存在数据 只需要获取当月的数据进行操作 获取当月我们自己的数据
                StatistOtherParametersMonth key = monthRepository.findByObjIdAndTypeAndMonthKey(2L, 20031L, years);
                SwStatistOtherParametersMonth sw1 = new SwStatistOtherParametersMonth();
                sw1.setTongTime(timestamp);
                sw1.setBizTime(new Date());
                sw1.setOp("insert");
                sw1.setAvgValue(key.getInAvgValue().toString());
                sw1.setMonthKey(years);
                swStatistOtherParametersMonthInfo.save(sw1);
            }
        }

    }

    public static String Years(String date) {
        SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date parse = null;
        try {
            parse = formats.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String format1 = format.format(parse);
        return format1;
    }

    /**
     * 坐标转换
     */
    public List<double[]> transReverseBatch(String paramJson) {
        ArrayList<double[]> results = new ArrayList<>();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity(paramJson, headers);
        Response s = restTemplate.postForObject(TRAN_URL, entity, Response.class);
        ArrayList a = (ArrayList) s.getData();
        for (Object o : a) {
            ArrayList result = (ArrayList) o;
            String x = result.get(0).toString();
            String y = result.get(1).toString();
            results.add(new double[]{Double.parseDouble(x), Double.parseDouble(y)});
            System.out.println(x + "::" + y);
        }
        return results;

    }
}
